<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace common\behaviors\backend;

use builder\base\BaseController;
use yii\base\Behavior;
use yii\base\Event;
use common\handlers\backend\BeforeActionValidateSSO;
use common\handlers\backend\BeforeActionValidateUserState;
use common\handlers\backend\BeforeActionValidateSite;

/**
 * 预执行控制器方法行为组件
 * @author cleverstone
 * @since ym2.0
 */
class BeforeActionBehaviors extends Behavior
{
    /**
     * {@inheritdoc}
     */
    public function attach($owner)
    {
        $this->owner = $owner;

        Event::on(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateSite(), 'run']);
        Event::on(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateUserState(), 'run']);
        Event::on(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateSSO(), 'run']);
    }

    /**
     * {@inheritdoc}
     */
    public function detach()
    {
        $this->owner = null;

        Event::off(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateSite(), 'run']);
        Event::off(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateUserState(), 'run']);
        Event::off(BaseController::class, BaseController::EVENT_BEFORE_ACTION, [new BeforeActionValidateSSO(), 'run']);
    }
}